<?php
  header("content-type:text/html;charset=utf8"); 
  //用户在微信绑定设备 进行账户注册并与微信进行绑定
  $tel = $_POST['tel'];
  $password = md5($_POST['password']);
  $openid = $_POST['openid'];
  if (!$openid) {
   echo "<script>alert('注册账户失败，可能因为网络原因导致，请关闭网页进行扫码重试');location.href='https://pay.hispot.cn/gitscan/h5/login.php'</script>";
   exit;
  }

  data_deposit_new_database($tel,$password,$openid);
  data_deposit_old_user_database($tel,$password,$openid);


  //将注册的数据存放到旧数据库中
  function data_deposit_old_user_database($tel,$password,$openid){
    //将注册数据存入旧数据库的登录信息表中
    $pdo_user_old = pdo_connect_old_user_database();
    $sql = "SELECT * FROM uc_members WHERE username = '$tel'";
    $data = $pdo_user_old->query($sql)->fetch(PDO::FETCH_ASSOC);
    if ($data) {
          // $delete_sql = "DELETE FROM uc_members WHERE username = '$tel'";
          // $pdo_user_old->exec($delete_sql);
          $uid = $data['uid'];
    }else{
          $salt = substr(md5(rand('1','1000')),0,6);
          $old_password = md5($password.$salt);
          $insert_old_user_sql = "INSERT INTO uc_members(username,password,salt,phone) VALUES('$tel','$old_password','$salt','$tel')";
          $pdo_user_old->exec($insert_old_user_sql);
          $uid = $pdo_user_old->lastInsertId();
    }

    //获取此登录信息所关联的绑定信息
    $pdo = pdo_connect_new_database();
    $select_binding_sql = "SELECT * FROM binding WHERE openid = '$openid' ORDER BY b_id DESC";
    $new_binding_data = $pdo->query($select_binding_sql)->fetch(PDO::FETCH_ASSOC);

    //将绑定信息存放到旧数据库的绑定信息表中
    $pdo_binding_old = pdo_connect_old_binidng_database();
    $time = date("Y-m-d H:i:s");
    $sn = $new_binding_data['device_sn'];
    $select_old_binding_sql = "SELECT * FROM weixin_binging WHERE Uid = '$uid' AND OpinID = '$openid' AND DeviceSelf = '$sn'";
    $old_binding_data = $pdo_binding_old->query($select_old_binding_sql)->fetch(PDO::FETCH_ASSOC);
    if (!$old_binding_data) {
        $insert_old_binidng_sql = "INSERT INTO weixin_binging(Uid,OpinID,CreateTime,DeviceSelf) VALUES('$uid','$openid','$time','$sn')";
        $pdo_binding_old->exec($insert_old_binidng_sql);   
    }

    //最后将uid和openid关联添加到weixin_user表中 添加之前先通过uid以及openid来查询关联数据是否已经存在
    $select_relation_data_sql = "SELECT * FROM weixin_user WHERE openId = '$openid' AND user = '$uid'";
    $relation_data = $pdo_binding_old->query($select_relation_data_sql)->fetch(PDO::FETCH_ASSOC);
    //如果数据不存在
    if (!$relation_data) {
      //将uid和openid关联存在weixin_user表中
      $insert_old_relation_sql = "INSERT INTO weixin_user(openId,user) VALUES('$openid','$uid')";
      $pdo_binding_old->exec($insert_old_relation_sql); 
    }
    file_get_contents("https://pay.hispot.cn/gitscan/pushLoginInfo.php?openid=".$openid);
    echo "<script>alert('恭喜您已经完成绑定操作，现在去设备端进行登录吧');location.href='https://pay.hispot.cn/gitscan/h5/success.php'</script>";
    exit;
  }

  //将注册的数据存放到新数据库中
  function data_deposit_new_database($tel,$password,$openid){
    $pdo = pdo_connect_new_database();
    $time = date("Y-m-d H:i:s");
    $insert_new_user_sql = "INSERT INTO user(tel,password,openid,`time`) VALUES('$tel','$password','$openid','$time')";
    $res = $pdo->exec($insert_new_user_sql);
    if (!$res) {
      echo "<script>alert('注册账户失败，可能因为网络原因导致，请检查网络并再次重试');location.href='https://pay.hispot.cn/gitscan/h5/register.php?openid=".$openid."'</script>";
       exit;
    }
  }

  //pdo连接新数据库
  function pdo_connect_new_database(){
    $pdo_new = new PDO('mysql:host=123.57.16.97;dbname=device_binding','root','zqVnUy6cxsQXbpEZ');
    $pdo_new->exec('set names utf8'); 
    return $pdo_new;
  }

  //pdo连接旧用户登录信息库
  function pdo_connect_old_user_database(){
    $pdo_user_old = new PDO('mysql:host=123.57.16.97;dbname=ucenter','root','zqVnUy6cxsQXbpEZ');
    $pdo_user_old->exec('set names utf8'); 
    return $pdo_user_old;
  }

  //pdo连接旧用户绑定表
  function pdo_connect_old_binidng_database(){
    $pdo_binding_old = new PDO('mysql:host=123.57.16.97;dbname=wx_online_movie','root','zqVnUy6cxsQXbpEZ');
    $pdo_binding_old->exec('set names utf8');
    return $pdo_binding_old;
  }
 
 ?>
